package com.onccc.social.step

import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CopyWriteModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class FacebookCreateSocial : StepImpl() {

    var mPackageName = "com.facebook.katana"

    private var writeModel = CopyWriteModel()

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动Facebook")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getOneTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }
            return@next Step.get(StepTag.STEP_12)
        }.next(StepTag.STEP_2) {
            runIO { delay(getOneTime()) }
            Assists.findByText("Create, Double tap to create a new post, story, or reel").firstOrNull() {
                OverManager.log("点击创建")
                Assists.gestureViewClick(it)
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_3)
            }
            runIO { delay(getOneTime()) }
            Assists.back()
            return@next Step.repeat
        }.next(StepTag.STEP_3) {
            runIO { delay(getOneTime()) }
            Assists.findByText("Reel").firstOrNull() {
                OverManager.log("选择视频发布")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_4)
            }
            Assists.back()
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_4) {
            runIO { delay(getOneTime()) }
            Assists.findByTags("android.widget.GridView").forEach {
                it.getChildren().forEach { item ->
                    if (item.childCount > 1) {
                        runIO { delay(getOneTime()) }
                        Assists.gestureViewClick(item)
                        OverManager.log("选中视频")
                        return@next Step.get(StepTag.STEP_5)
                    }
                }

            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_5) {
            runIO { delay(getOneTime()) }
            Assists.findByText("Next").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("选择视频下一步")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_6)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }

            Assists.findByTags("android.widget.EditText").forEach {
                runIO { delay(getOneTime()) }
                Assists.gestureViewClick(it)
                if (!it.isFocusable){
                    runIO { delay(getOneTime()) }
                    Assists.gestureViewClick(it)
                }
                return@next Step.get(StepTag.STEP_9)
            }


            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_9) {
            MainActivity.getPostContent("facebook", { model ->
                writeModel = model
                StepManager.execute(FacebookCreateSocial::class.java, StepTag.STEP_10, begin = true)
            })
            {
                StepManager.execute(FacebookCreateSocial::class.java, StepTag.STEP_10, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_10) {
            runIO { delay(getOneTime()) }
            OverManager.log("点击输入框")
            Assists.findByTags("android.widget.EditText").forEach {
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)

                return@next Step.get(StepTag.STEP_11)
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }
            OverManager.log("输入发送内容")
            Assists.findByTags("android.widget.EditText").forEach {
//                val editNode =  it.getChild(0).getChild(0).getChild(1).getChild(0).getChild(0)
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)

                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                for (s in writeModel.copywriting) {
                    it.paste(s.toString())
                    runIO { delay(getInputTime()) }
                }
                if (writeModel.hashtags.size == 0) {
                    return@next Step.get(StepTag.STEP_12)
                }
                var hashtag = ""
                for (s in writeModel.hashtags) {
                    hashtag += "#$s "
                }
                for (hs in hashtag) {
                    it.paste(hs.toString())
                    runIO { delay(getInputTime()) }
                }

                return@next Step.get(StepTag.STEP_12)
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_12) {
            runIO { delay(getOneTime()) }
            Assists.findByText("Share now").forEach() {
                it.log()
                OverManager.log("确定发送")
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_100)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_97) {
            runIO { delay(getOneTime()) }
            Assists.back()
            return@next Step.get(StepTag.STEP_98)
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}